import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 1.2 as OldControl
//import mytablemodel 1.0
import Qt.labs.qmlmodels 1.0

MainView {
    id: root
    property string tableName: "weightTable"
    property int verHeaderHeight: 30
    property int verHeaderWidth: 0
    property int horHeaderHeight: 30
    property variant columnWidthArr: [205,205,130,130,130]
    property string m_startTime:Qt.formatDateTime(new Date(), "yyyy-MM-dd 00:00:00")
    property string m_endTime:Qt.formatDateTime(new Date(), "yyyy-MM-dd 23:59:59")
    property int m_currentPage:0
//    property int m_startYear : new Date().getFullYear();
//    property int m_startMonth: new Date().getMonth()+1;
//    property int m_startDay: new Date().getDate();
    //property string strStartTime:""
    //property string strEndTime:""

    //m_startYear.toString()+"-"+m_startMonth.toString().padStart(2, '0')+"-"+m_startDay.toString()+" 23:59:59"
    property int btnWidth:30
    property int btnHeight:50
    signal signalRefresh
    property int dateType:0
    //property int currentPage: 0
    property int totalPage:1
    width: 800
    height: 391//

    onVisibleChanged: {
    }
//    function onWeightDatasChanged()
//    {
//        tableView.forceLayout()
//    }
    //qsTr("日期"), qsTr("重量"),qsTr("经度"), qsTr("纬度")
    function getTitle(index)
    {
        switch(index)
        {
            case 0:
                return qsTr("日期")
            case 1:
                return qsTr("重量")
            case 2:
                return qsTr("经度")
            case 3:
                return qsTr("纬度")
            case 4:
                return qsTr("高度")
        }
    }
Component.onCompleted:
{
    //mymodbus.weightDatasChanged.connect(onWeightDatasChanged)
    signalRefresh();
    myTableModel.rows = Qt.binding(function() { return mymodbus.weightDatas })
    //tableView.visible = true;
}

    Dialog {
        id: dateDialog
        anchors.centerIn: parent
        modal: Qt.WindowModal
        title: dateType==0?qsTr("开始日期"):qsTr("结束日期")
        standardButtons: Dialog.Ok
        //onButtonClicked: console.log("clicked button " + clickedButton)
        onAccepted: {
            var date0=calendar.selectedDate
            if(dateType==0){
                startDate.text=Qt.formatDateTime(date0, "yyyy-MM-dd")
            }
            else{
                endDate.text=Qt.formatDateTime(date0, "yyyy-MM-dd")
            }
        }
//        OldControl.Calendar {
//            id: calendar
//            width: parent ? parent.width : implicitWidth
//            onDoubleClicked: dateDialog.accept()
//            locale: mytables.language===0?Qt.locale("zh_CN"):Qt.locale("en_US")
//        }
    }
    DateTimeDialog{
            id: dlg
            text: dateType==0?qsTr("开始日期"):qsTr("结束日期")
            type: "Date"
            onAccepted: {
//                console.log(getText("yyyy-MM-dd hh:mm:ss"));
//                console.log(getText("yyyy-MM-dd"));
//                console.log(getText("hh:mm:ss"));
                if(dateType==0){
                    startDate.text=getText("yyyy-MM-dd")
                }
                else{
                    endDate.text=getText("yyyy-MM-dd")
                }
            }
        }

Rectangle
   {
       width:800
       height: 50
       color:"transparent"
       GridLayout{ //root中放置一个行布局，布局中放置3个方块
           id: zeroCalibrationLayout
           anchors.fill: parent
           //columnSpacing: 20 //间距
           rows: 1//行数
           columns: 4 //列数
               Button
               {
                   id:startDate
                   // x: 104
                   // y: 0
                   Layout.row:0
                   Layout.column:0
                   Layout.preferredWidth: 310
                   Layout.preferredHeight: btnHeight
                   text:m_startTime.slice(0,10)
                       //Qt.formatDateTime(new Date(), "yyyy-MM-dd")
                   background:
                       Rectangle{
                       border.width: 0
                       color: "#7584C2"
                           //startDate.down?"#388C8C":
                           }
                   onClicked: {
//                       dataLoader.running=true
//                       dataLoader.visible=true;
                       //dataLoader.
                       dateType=0
                       //var locale = Qt.locale()
                       //var date1 = Date.fromLocaleString(locale, startDate.text, "yyyy-MM-dd");
                       //calendar.selectedDate=date1
                       //dateDialog.open()
                       dlg.open()
                       dlg.exec(startDate.text)
                       //dlg.exec(Qt.formatDateTime(new Date(), "yyyy-MM-dd"))
                       //dataLoader.visible=false;
                       //dataLoader.running=false
                   }
                   palette.buttonText: "white"
               }
                   Text
                   {
                       // x: 325
                       // y: 0
                       Layout.row:0
                       Layout.column:1
                       Layout.preferredWidth: btnWidth
                       Layout.preferredHeight: btnHeight
                       text:qsTr("至")
                       font.pixelSize: 25
                       color: "white"
                       verticalAlignment: Text.AlignVCenter
                   }
                   Button
                   {
                       id:endDate
                       // x: 104
                       // y: 0
                       Layout.row:0
                       Layout.column:2
                       Layout.preferredWidth: 310
                       Layout.preferredHeight: btnHeight
                       text:m_endTime.slice(0,10)
                           //Qt.formatDateTime(new Date(), "yyyy-MM-dd")
                       background:
                           Rectangle{
                           border.width: 0
                           color: "#7584C2"
                               }
                       palette.buttonText: "white"
                       onClicked: {
                           dateType=1
//                           var locale = Qt.locale()
//                           var date1 = Date.fromLocaleString(locale, endDate.text, "yyyy-MM-dd");
//                           calendar.selectedDate=date1
//                           dateDialog.open()
                           dlg.open()
                           dlg.exec(endDate.text)
                           //dlg.exec(Qt.formatDateTime(new Date(), "yyyy-MM-dd"))
                       }
                   }
                   Button
                   {
                       id:btnQuery
                       // x: 700
                       // y: 0
                       // width: 95
                       // height: 45
                       Layout.row:0
                       Layout.column:3
                       //Layout.preferredWidth: 95
                       Layout.preferredHeight: 45
                       background: Rectangle{
                           border.width: 0
                           color: "#C070B0"
                               }
                       palette.buttonText: "white"
                       text: qsTr("查询")
                       font.pixelSize: 25
                       onClicked: {
                           if(dataLoader.visible){
                               return
                           }
                           dataLoader.visible = true
                           dataTimmer.start()
                           m_startTime=startDate.text+" 00:00:00"
                           mymodbus.setStartTime(m_startTime);
                           m_endTime=endDate.text+" 23:59:59"
                           mymodbus.setEndTime(m_endTime);
                           m_currentPage = 0
                           mymodbus.setCurrentPage(m_currentPage);
                           //MyTableModel.slotParaChanged(m_startTime,m_endTime,m_currentPage)
                           totalPage = mymodbus.totalCount(m_startTime,m_endTime)
                           pageNum.text=(m_currentPage+1)+"/"+(totalPage)

                   }
               }
       }
  }
    TableView {
        id: tableView
        anchors.leftMargin: -1
        anchors.rightMargin: 1
        anchors.topMargin: 50+root.horHeaderHeight
        anchors.bottomMargin: 43

            columnSpacing: 1
            columnWidthProvider: function (column) {
                return root.columnWidthArr[column];
            }
            rowHeightProvider: function (row) {
                return root.verHeaderHeight;
            }
            Layout.margins: 0
            anchors{
                fill: parent
            }
            model: TableModel
                   {
                       id:myTableModel
                       TableModelColumn { display: "createdTime" }
                       TableModelColumn { display: "strWeight" }
                       TableModelColumn { display: "strLon" }
                       TableModelColumn { display: "strLat" }
                       TableModelColumn { display: "strHeight" }

                       //rows:{}
                   }
            //model: mymodbus.weightDatas
                //MyTableModel{startTime:m_startTime; endTime: m_endTime;currentPage: m_currentPage;}
            //OldControl.TableViewColumn
            //{
            //    role: "createdTime"
            //    title:getTitle(0)
            //    width: 100
            //}
            //clip: true
            delegate: Rectangle {
                color:"#0A3653"
                width: tableView.columnWidthProvider(model.row)
                height: horHeaderHeight
                anchors.margins: -1
                border.color:"white"
                Label {
                    id: label
                    text: display
                    anchors.fill: parent
                    anchors.margins: 0
                    color: "white"
                    font.pixelSize: 20
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                }
            }
        }
        //定义横向表头
    Item{
        id: header_horizontal
        anchors{
            left: parent.left
            right: parent.right
            leftMargin: root.verHeaderWidth
            top: parent.top
            topMargin: 50
        }
        height: root.horHeaderHeight
        z: 2

        Row {
            id: header_horizontal_row
            anchors.fill: parent
            leftPadding: -tableView.contentX
            clip: true
            spacing: 0

            Repeater {
                model: [qsTr("日期"), qsTr("重量"),qsTr("经度"), qsTr("纬度"), qsTr("高度")]
                    //tableView.columns > 0 ? tableView.columns : 0

                Rectangle {
                    id: header_horizontal_item
                    width: tableView.columnWidthProvider(index)+tableView.columnSpacing
                    height: root.horHeaderHeight
                    color: "#0A3653"
                    border.color:"white"

                    Text {
                        anchors.centerIn: parent
                        text: getTitle(index)
                            //index===0?qsTr("日期"):qsTr("重量")
                            //tableView.model.headerData(index, Qt.Horizontal)
                        color: "white"
                    }
                    Rectangle{
                        width: 1
                        height: parent.height
                        anchors.right: parent.right
                        color: "black"
                        opacity: 0.5
                    }
                }
            }
        }
    }
    Timer{
        id:dataTimmer
        interval: 200
        running: false
        repeat: false
        onTriggered: {dataLoader.visible = false}
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: dataLoader
        visible:false
        //anchors.verticalCenterOffset: 3
        //anchors.horizontalCenterOffset: 22
    }
    Rectangle{
        x: -1
        y: 330
        height:45
        width: 800
        color:"transparent"

        Row {
            anchors.centerIn: parent
            spacing: 10
            Button {
                id: btnFirstPage
                height: 40
                width: 123
                text: qsTr("首页")
                font.pixelSize: 25
                palette.buttonText: "white"
                onClicked: {
                    if(dataLoader.visible){
                        return
                    }
                    dataLoader.visible = true
                    dataTimmer.start()
                    m_startTime=startDate.text+" 00:00:00"
                    mymodbus.setStartTime(m_startTime)
                    m_endTime=endDate.text+" 23:59:59"
                    mymodbus.setEndTime(m_endTime)
                    m_currentPage = 0
                    mymodbus.setCurrentPage(m_currentPage);
                    pageNum.text=(m_currentPage+1)+"/"+(totalPage)
                    //tableView.forceLayout()
                }
                background: Rectangle {
                    color: "#7584c2"
                    border.width: 0
                }
            }
            Button {
                id: btnPrePage
                height: 40
                width: 123
                text: qsTr("上一页")
                font.pixelSize: 25
                palette.buttonText: "white"
                onClicked: {
                    if(dataLoader.visible){
                        return
                    }
                    dataLoader.visible = true
                    dataTimmer.start()
                    m_startTime=startDate.text+" 00:00:00"
                    mymodbus.setStartTime(m_startTime)
                    m_endTime=endDate.text+" 23:59:59"
                    mymodbus.setEndTime(m_endTime)
                    if(m_currentPage > 0)
                    {
                        m_currentPage = m_currentPage - 1;
                        mymodbus.setCurrentPage(m_currentPage);
                    }
                    pageNum.text=(m_currentPage+1)+"/"+(totalPage)
                    //tableView.forceLayout()
                }
                background: Rectangle {
                    color: "#7584c2"
                    border.width: 0
                }
            }
            Text{
                id:pageNum
                font.pixelSize: 38
                height:40
                color:"White"
                text:(m_currentPage+1)+"/"+(totalPage)
            }

            Button {
                id: btnNextPage
                height: 40
                width: 123
                text: qsTr("下一页")
                font.pixelSize: 25
                palette.buttonText: "white"
                onClicked: {
                    if(dataLoader.visible){
                        return
                    }
                    dataLoader.visible = true
                    dataTimmer.start()
                    m_startTime=startDate.text+" 00:00:00"
                    mymodbus.setStartTime(m_startTime)
                    m_endTime=endDate.text+" 23:59:59"
                    mymodbus.setEndTime(m_endTime)
                    if(m_currentPage<totalPage-1)
                    {
                        m_currentPage = m_currentPage + 1;
                        mymodbus.setCurrentPage(m_currentPage);
                    }
                    //tablemodel.slotParaChanged(m_startTime,m_endTime,m_currentPage)
                    pageNum.text=(m_currentPage+1)+"/"+(totalPage)
                    //tableView.forceLayout()
                }
                background: Rectangle {
                    color: "#7584c2"
                    border.width: 0
                }
            }
            Button {
                id: btnLastPage
                height: 40
                width: 123
                text: qsTr("尾页")
                font.pixelSize: 25
                palette.buttonText: "white"
                onClicked: {
                    if(dataLoader.visible){
                        return
                    }
                dataLoader.visible = true
                dataTimmer.start()
                m_startTime=startDate.text+" 00:00:00"
                mymodbus.setStartTime(m_startTime)
                m_endTime=endDate.text+" 23:59:59"
                mymodbus.setEndTime(m_endTime)
                m_currentPage = totalPage-1;
                mymodbus.setCurrentPage(m_currentPage);
                pageNum.text=(m_currentPage+1)+"/"+(totalPage)
                //tableView.forceLayout()
                }
                background: Rectangle {
                    color: "#7584c2"
                    border.width: 0
                }
            }
        }

    }
    onSignalRefresh: {
        //MyTableModel.slotTimeChanged(m_startTime,m_endTime)
        //dataLoader.visible = true
        //dataTimmer.start()
        //m_currentPage = 1
        //m_currentPage = 0
        mymodbus.setStartTime(m_startTime);
        mymodbus.setEndTime(m_endTime);
        mymodbus.setCurrentPage(m_currentPage);
        totalPage = mymodbus.totalCount(m_startTime,m_endTime)
        pageNum.text=(m_currentPage+1).toString()+"/"+(totalPage).toString()
        //tableView.forceLayout()
        //btnQuery.clicked()

    }
}

